<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Metadata</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.quickstart.transactions.html">API support for transactions</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.setup.html">安装／配置</a></div>
 <div class="up"><a href="mysqli.quickstart.html">Quick start guide</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqli.quickstart.metadata" class="section">
  <h2 class="title">Metadata</h2>
  <p class="para">
   A MySQL result set contains metadata. The metadata describes the columns
   found in the result set. All metadata sent by MySQL is accessible
   through the <em>mysqli</em> interface.
   The extension performs no or negligible changes to the
   information it receives.
   Differences between MySQL server versions are not aligned.
  </p>
  <p class="para">
   Meta data is access through the <a href="class.mysqli-result.html" class="classname">mysqli_result</a> interface.
  </p>
  <p class="para">
   <div class="example" id="example-1615">
    <p><strong>Example #1 Accessing result set meta data</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_errno</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Failed&nbsp;to&nbsp;connect&nbsp;to&nbsp;MySQL:&nbsp;("&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_errno&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">")&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_error</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;1&nbsp;AS&nbsp;_one,&nbsp;'Hello'&nbsp;AS&nbsp;_two&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_fields</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [0]=&gt;
  object(stdClass)#3 (13) {
    [&quot;name&quot;]=&gt;
    string(4) &quot;_one&quot;
    [&quot;orgname&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;table&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;orgtable&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;def&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;db&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;catalog&quot;]=&gt;
    string(3) &quot;def&quot;
    [&quot;max_length&quot;]=&gt;
    int(1)
    [&quot;length&quot;]=&gt;
    int(1)
    [&quot;charsetnr&quot;]=&gt;
    int(63)
    [&quot;flags&quot;]=&gt;
    int(32897)
    [&quot;type&quot;]=&gt;
    int(8)
    [&quot;decimals&quot;]=&gt;
    int(0)
  }
  [1]=&gt;
  object(stdClass)#4 (13) {
    [&quot;name&quot;]=&gt;
    string(4) &quot;_two&quot;
    [&quot;orgname&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;table&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;orgtable&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;def&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;db&quot;]=&gt;
    string(0) &quot;&quot;
    [&quot;catalog&quot;]=&gt;
    string(3) &quot;def&quot;
    [&quot;max_length&quot;]=&gt;
    int(5)
    [&quot;length&quot;]=&gt;
    int(5)
    [&quot;charsetnr&quot;]=&gt;
    int(8)
    [&quot;flags&quot;]=&gt;
    int(1)
    [&quot;type&quot;]=&gt;
    int(253)
    [&quot;decimals&quot;]=&gt;
    int(31)
  }
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <em class="emphasis">Prepared statements</em>
  </p>
  <p class="para">
   Meta data of result sets created using prepared statements are accessed
   the same way. A suitable <a href="class.mysqli-result.html" class="classname">mysqli_result</a> handle is
   returned by <span class="function"><a href="mysqli-stmt.result-metadata.html" class="function">mysqli_stmt_result_metadata()</a></span>.
  </p>
  <p class="para">
   <div class="example" id="example-1616">
    <p><strong>Example #2 Prepared statements metadata</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;1&nbsp;AS&nbsp;_one,&nbsp;'Hello'&nbsp;AS&nbsp;_two&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">result_metadata</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_fields</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <em class="emphasis">See also</em>
  </p>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="mysqli.query.html" class="methodname">mysqli::query()</a></span></li>
    <li class="member"><span class="methodname"><a href="mysqli-result.fetch-fields.html" class="methodname">mysqli_result::fetch_fields()</a></span></li>
   </ul>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.quickstart.transactions.html">API support for transactions</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.setup.html">安装／配置</a></div>
 <div class="up"><a href="mysqli.quickstart.html">Quick start guide</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
